
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;


using Yozeev.SystemConfig.BasicConfig;
using Yozeev.SystemConfig.BasicConfig.Dtos;
using Yozeev.SystemConfig.BasicConfig.DomainService;
using Yozeev.SystemConfig.BasicConfig.Authorization;
using Yozeev.Common;

namespace Yozeev.SystemConfig.BasicConfig
{
    /// <summary>
    /// DateDictionary应用层服务的接口实现方法  
    ///</summary>
    [AbpAuthorize]
    public class DateDictionaryAppService : YozeevAppServiceBase, IDateDictionaryAppService
    {
        private readonly IRepository<DateDictionary, Guid> _entityRepository;

        private readonly IDateDictionaryManager _entityManager;

		private readonly IQueryHelper _queryHelper;

		/// <summary>
		/// 构造函数 
		///</summary>
		public DateDictionaryAppService(
        IRepository<DateDictionary, Guid> entityRepository
        ,IDateDictionaryManager entityManager
			, IQueryHelper queryHelper
        )
        {
            _entityRepository = entityRepository; 
             _entityManager=entityManager;
			_queryHelper = queryHelper;
        }


        /// <summary>
        /// 获取DateDictionary的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<DateDictionaryListDto>> GetPaged(GetDateDictionarysInput input)
		{
			var expression = _queryHelper.WhereExp<DateDictionary>(input.queryData);
			var query = from dateDictionary in _entityRepository.GetAll().AsNoTracking()
						.Where(expression)
						select new
						{
							dateDictionary,
							dateDictionary.Parent,
						};
			var list = new List<DateDictionary>();
			query.ToList().ForEach(temp => {
				var _dateDictionary = temp.dateDictionary;
				_dateDictionary.Parent = temp.Parent;
				list.Add(_dateDictionary);
			});
			var count = list.Count();
			var entityList = list;
			var entityListDtos =entityList.MapTo<List<DateDictionaryListDto>>();
			return new PagedResultDto<DateDictionaryListDto>(count,entityListDtos);
		}


		/// <summary>
		/// 通过指定id获取DateDictionaryListDto信息
		/// </summary>
		public async Task<DateDictionaryListDto> GetById(EntityDto<Guid> input)
		{
			var entity = await _entityRepository.GetAsync(input.Id);

		    return entity.MapTo<DateDictionaryListDto>();
		}

		/// <summary>
		/// 获取编辑 DateDictionary
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task<GetDateDictionaryForEditOutput> GetForEdit(NullableIdDto<Guid> input)
		{
			var output = new GetDateDictionaryForEditOutput();
DateDictionaryEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _entityRepository.GetAsync(input.Id.Value);

				editDto = entity.MapTo<DateDictionaryEditDto>();

				//dateDictionaryEditDto = ObjectMapper.Map<List<dateDictionaryEditDto>>(entity);
			}
			else
			{
				editDto = new DateDictionaryEditDto();
			}

			output.DateDictionary = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改DateDictionary的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task CreateOrUpdate(CreateOrUpdateDateDictionaryInput input)
		{

			if (input.DateDictionary.Id.HasValue)
			{
				await Update(input.DateDictionary);
			}
			else
			{
				await Create(input.DateDictionary);
			}
		}


		/// <summary>
		/// 新增DateDictionary
		/// </summary>
		protected virtual async Task<DateDictionaryEditDto> Create(DateDictionaryEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

            // var entity = ObjectMapper.Map <DateDictionary>(input);
            var entity=input.MapTo<DateDictionary>();
			

			entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<DateDictionaryEditDto>();
		}

		/// <summary>
		/// 编辑DateDictionary
		/// </summary>
		protected virtual async Task Update(DateDictionaryEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

			var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);

			// ObjectMapper.Map(input, entity);
		    await _entityRepository.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除DateDictionary信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task Delete(EntityDto<Guid> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除DateDictionary的方法
		/// </summary>
		public async Task BatchDelete(List<Guid> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		/// <summary>
		/// 导出DateDictionary为excel表,等待开发。
		/// </summary>
		/// <returns></returns>
		//public async Task<FileDto> GetToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}

		/// <summary>
		/// 获取打印样式绑定值列表
		/// </summary>
		/// <param name="TicketBinding">字典中的代码</param>
		/// <returns></returns>
		public async Task<List<DateDictionary>> GetTicketBinding(string TicketBinding = "TicketBinding")
		{
			var list = await _entityRepository.GetAllListAsync(m => m.Parent.DictionaryCode == TicketBinding);
			return list.ToList();
		}
	}
}


